package com.csb.controller;

import com.csb.pojo.Apply;
import com.csb.pojo.UserInfo;
import com.csb.service.ApplyService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/apply")
public class ApplyController {
    // 依赖
    @Autowired
    private ApplyService applyService;

    /**
     * 申请
     * @param asIds
     * @return
     */
    @RequestMapping("/app")
    public String appApplyInfo(String[] asIds) {
        // 获取账号
        UserInfo userInfo = (UserInfo) SecurityUtils.getSubject().getPrincipal();
        String account = userInfo.getAccount();
        //判断
        if (applyService.saveApplyInfo(asIds,account)) {
            return "1";
        } else {
            return "0";
        }
    }

    /**
     * 归还
     * @param asIds
     * @return
     */
    @RequestMapping("/back")
    public String backApplyInfo(String[] asIds) {
        // 获取账号
        UserInfo userInfo = (UserInfo) SecurityUtils.getSubject().getPrincipal();
        String account = userInfo.getAccount();
        //判断
        if (applyService.backApplyInfo(asIds,account)) {
            return "1";
        } else {
            return "0";
        }
    }

    /**
     * 获取个人申请列表
     * @return
     */
    @RequestMapping("/personList")
    public List<Apply> personList(){
        // 获取账号
        UserInfo userInfo = (UserInfo) SecurityUtils.getSubject().getPrincipal();
        if (userInfo==null){
            return null;
        }
        String account = userInfo.getAccount();
        //获取个人的申请列表
        List<Apply> personLst= applyService.getPersonAppList(account);
        return personLst;
    }

    /**
     * 获取所有申请列表
     * @return
     */
    @RequestMapping("/personAllList")
    public List<Apply> personAllList(){
        // 获取所有的申请列表
        List<Apply> personLst= applyService.getPersonAppAllList();
        // 返回
        return personLst;
    }

}
